﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;

    public class POD : BasePage
    {
        protected Button btnConfirm;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnSave;
        protected Button btnUpLoadPOD;
        protected DropDownList ddlPickUpType;
        protected DropDownList ddlStatus;
        protected DropDownList ddlTransportMode;
        protected HtmlForm F8051;
        protected GridView gvPodDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnPODImg;
        protected Image Image1;
        protected Image Image2;
        protected Image Image3;
        protected Image Image4;
        protected Image Image5;
        protected Image Image6;
        protected Label lblBolDetailID;
        protected Label lblCAddress;
        protected Label lblCarrierID;
        protected Label lblContact;
        protected Label lblCPhone;
        protected Label lblCreatedBy;
        protected Label lblCreatedDate;
        protected Label lblCustomerID;
        protected Label lblCustomerName;
        protected Label lblDriver;
        protected Label lblEquipmentID;
        protected Label lblExpectArriverDate;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblPickUpType;
        protected Label lblPodID;
        protected Label lblRemark;
        protected Label lblSignedBy;
        protected Label lblSignedDate;
        protected Label lblStatus;
        protected Label lblTransportMode;
        protected Label lblUdf04;
        protected Label lblUdf09;
        protected Label lblUdf1;
        protected Label lblUdf10;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf5;
        protected Label lblUdf6;
        protected Label lblUdf7;
        protected Label lblUdf8;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Label one4;
        protected ScriptManager ScriptManager1;
        protected TextBox txtBolDetailID;
        protected TextBox txtCAddress;
        protected TextBox txtCarrierID;
        protected TextBox txtContact;
        protected TextBox txtCPhone;
        protected TextBox txtCreatedBy;
        protected TextBox txtCreatedDate;
        protected TextBox txtCustomerID;
        protected TextBox txtCustomerName;
        protected TextBox txtDriver;
        protected TextBox txtEquipmentID;
        protected TextBox txtExpectArriverDate;
        protected TextBox txtOwnerID;
        protected TextBox txtPodID;
        protected TextBox txtRemark;
        protected TextBox txtSignedBy;
        protected TextBox txtSignedDate;
        protected TextBox txtUdf1;
        protected TextBox txtUdf10;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtUdf6;
        protected TextBox txtUdf7;
        protected TextBox txtUdf8;
        protected TextBox txtUdf9;

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                PodInfo podInfo = base.DataEntityViewState as PodInfo;
                podInfo = this.GetPodInfo(podInfo);
                podInfo.Status = "POD";
                if ((podInfo.PodDetailInfos != null) && (podInfo.PodDetailInfos.Count > 0))
                {
                    for (int i = 0; i < podInfo.PodDetailInfos.Count; i++)
                    {
                        podInfo.PodDetailInfos[i].PodID = podInfo.PodID;
                        podInfo.PodDetailInfos[i].Status = "POD";
                        podInfo.PodDetailInfos[i].UpdatedBy = base.UserInfo.UserID;
                    }
                }
                podInfo.UpdatedBy = base.UserInfo.UserID;
                string temp = TransportMgtServiceClientProxy.GetInstance().ConfirmPOD(podInfo);
                this.SetPod2Form();
                this.lblMsg.Text = temp + base.GetGlobalMsgResource("F8500_003");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8500_004") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);</script>", false);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("PODQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("PODList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtSignedDate.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8500_001");
            }
            else if (string.IsNullOrEmpty(this.txtSignedBy.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F8500_002");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdatePodInfo();
            }
            else
            {
                this.InsertPodInfo();
            }
        }

        protected void btnUpLoadPOD_Click(object sender, EventArgs e)
        {
            try
            {
                PodInfo podInfo = base.DataEntityViewState as PodInfo;
                podInfo.PodID = this.txtPodID.Text.Trim();
                if (!string.IsNullOrEmpty(this.hdnPODImg.Value))
                {
                    podInfo.Podimg = this.hdnPODImg.Value;
                }
                podInfo.UpdatedBy = base.UserInfo.UserID;
                string temp = TransportMgtServiceClientProxy.GetInstance().UpdatePodInfo(podInfo);
                this.SetPod2Form();
                this.lblMsg.Text = temp + base.GetGlobalMsgResource("C004");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',1,4);</script>", false);
            }
        }

        private List<PodDetailInfo> GetPodDetailFromGvPodDetail()
        {
            decimal tempDec = 0M;
            decimal Qty = 0M;
            decimal AcceptedQty = 0M;
            decimal RejectedQty = 0M;
            decimal LoseQty = 0M;
            decimal BreakageQty = 0M;
            decimal PkgBreakageQty = 0M;
            List<PodDetailInfo> podDetailInfos = new List<PodDetailInfo>();
            for (int i = 0; i < this.gvPodDetail.Rows.Count; i++)
            {
                Qty = 0M;
                AcceptedQty = 0M;
                RejectedQty = 0M;
                LoseQty = 0M;
                BreakageQty = 0M;
                PkgBreakageQty = 0M;
                PodDetailInfo podDetail = new PodDetailInfo {
                    PodID = (this.gvPodDetail.DataKeys[i]["PODID"] != null) ? this.gvPodDetail.DataKeys[i]["PODID"].ToString() : string.Empty,
                    LineID = (this.gvPodDetail.DataKeys[i]["LineID"] != null) ? int.Parse(this.gvPodDetail.DataKeys[i]["LineID"].ToString()) : 10,
                    OwnerID = (this.gvPodDetail.DataKeys[i]["OwnerID"] != null) ? this.gvPodDetail.DataKeys[i]["OwnerID"].ToString() : string.Empty,
                    SkuID = (this.gvPodDetail.DataKeys[i]["SkuID"] != null) ? this.gvPodDetail.DataKeys[i]["SkuID"].ToString() : string.Empty,
                    PackID = (this.gvPodDetail.DataKeys[i]["PackID"] != null) ? this.gvPodDetail.DataKeys[i]["PackID"].ToString() : string.Empty,
                    BolLineID = (this.gvPodDetail.DataKeys[i]["BolLineID"] != null) ? int.Parse(this.gvPodDetail.DataKeys[i]["BolLineID"].ToString()) : 10,
                    DeliveryID = (this.gvPodDetail.DataKeys[i]["DeliveryID"] != null) ? this.gvPodDetail.DataKeys[i]["DeliveryID"].ToString() : string.Empty,
                    DeliveryLineID = (this.gvPodDetail.DataKeys[i]["DeliveryLineID"] != null) ? int.Parse(this.gvPodDetail.DataKeys[i]["DeliveryLineID"].ToString()) : 10
                };
                if (decimal.TryParse(((Label) this.gvPodDetail.Rows[i].FindControl("lblQty")).Text, out tempDec))
                {
                    podDetail.Qty = decimal.Parse(((Label) this.gvPodDetail.Rows[i].FindControl("lblQty")).Text);
                    Qty = podDetail.Qty;
                }
                else
                {
                    podDetail.Qty = tempDec;
                }
                if (decimal.TryParse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtAcceptedQty")).Text, out tempDec))
                {
                    podDetail.AcceptedQty = decimal.Parse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtAcceptedQty")).Text);
                    AcceptedQty = podDetail.AcceptedQty;
                }
                else
                {
                    podDetail.AcceptedQty = tempDec;
                }
                if (decimal.TryParse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtRejectedQty")).Text, out tempDec))
                {
                    podDetail.RejectedQty = decimal.Parse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtRejectedQty")).Text);
                    RejectedQty = podDetail.RejectedQty;
                }
                else
                {
                    podDetail.RejectedQty = tempDec;
                }
                if (decimal.TryParse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtLoseQty")).Text, out tempDec))
                {
                    podDetail.LoseQty = decimal.Parse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtLoseQty")).Text);
                    LoseQty = podDetail.LoseQty;
                }
                else
                {
                    podDetail.LoseQty = tempDec;
                }
                if (decimal.TryParse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtBreakageQty")).Text, out tempDec))
                {
                    podDetail.BreakageQty = decimal.Parse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtBreakageQty")).Text);
                    BreakageQty = podDetail.BreakageQty;
                }
                else
                {
                    podDetail.BreakageQty = tempDec;
                }
                if (decimal.TryParse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtPkgBreakageQty")).Text, out tempDec))
                {
                    podDetail.PkgBreakageQty = decimal.Parse(((TextBox) this.gvPodDetail.Rows[i].FindControl("txtPkgBreakageQty")).Text);
                    PkgBreakageQty = podDetail.PkgBreakageQty;
                }
                else
                {
                    podDetail.PkgBreakageQty = tempDec;
                }
                if (((AcceptedQty + RejectedQty) + LoseQty) != Qty)
                {
                    throw new Exception(podDetail.LineID.ToString() + ",的实际收货数量和拒收数量已经丢失数量必须等于送货数量");
                }
                if ((BreakageQty + PkgBreakageQty) > AcceptedQty)
                {
                    throw new Exception(podDetail.LineID.ToString() + ",破损数量和外包装破损数量总和不能大于实际接受数量");
                }
                podDetail.CreatedBy = base.UserInfo.UserID;
                podDetail.CreatedDate = DateTime.Now;
                podDetailInfos.Add(podDetail);
            }
            return podDetailInfos;
        }

        private PodInfo GetPodInfo(PodInfo podInfo)
        {
            DateTime tempDateTime = DateTime.Now;
            podInfo.PodID = this.txtPodID.Text.Trim();
            podInfo.BolID = this.txtBolDetailID.Text.Trim();
            podInfo.CarrierID = this.txtCarrierID.Text.Trim();
            podInfo.OwnerID = this.txtOwnerID.Text.Trim();
            podInfo.EquipmentID = this.txtEquipmentID.Text.Trim();
            podInfo.DriverID = this.txtDriver.Text.Trim();
            if (DateTime.TryParse(this.txtExpectArriverDate.Text.Trim(), out tempDateTime))
            {
                podInfo.ExceptArriverdDate = DateTime.Parse(this.txtExpectArriverDate.Text.Trim());
            }
            else
            {
                podInfo.ExceptArriverdDate = DateTime.Now;
            }
            if (DateTime.TryParse(this.txtSignedDate.Text.Trim(), out tempDateTime))
            {
                podInfo.SignedDate = DateTime.Parse(this.txtSignedDate.Text.Trim());
            }
            else
            {
                podInfo.SignedDate = DateTime.Now;
            }
            podInfo.SignedBy = this.txtSignedBy.Text.Trim();
            podInfo.Status = this.ddlStatus.SelectedValue;
            podInfo.TransportMode = this.ddlTransportMode.SelectedValue;
            podInfo.PickupType = this.ddlPickUpType.SelectedValue;
            podInfo.CustomerID = this.txtCustomerID.Text.Trim();
            podInfo.CustomerName = this.txtCustomerName.Text.Trim();
            podInfo.Contact = this.txtContact.Text.Trim();
            podInfo.CPhone = this.txtCPhone.Text.Trim();
            podInfo.CAddress = this.txtCAddress.Text.Trim();
            podInfo.Udf1 = this.txtUdf1.Text.Trim();
            podInfo.Udf2 = this.txtUdf2.Text.Trim();
            podInfo.Udf3 = this.txtUdf3.Text.Trim();
            podInfo.Udf4 = this.txtUdf4.Text.Trim();
            podInfo.Udf5 = this.txtUdf5.Text.Trim();
            podInfo.Udf6 = this.txtUdf6.Text.Trim();
            podInfo.Udf7 = this.txtUdf7.Text.Trim();
            podInfo.Udf8 = this.txtUdf8.Text.Trim();
            podInfo.Udf9 = this.txtUdf9.Text.Trim();
            podInfo.Udf10 = this.txtUdf10.Text.Trim();
            podInfo.CreatedBy = this.txtCreatedBy.Text.Trim();
            if (DateTime.TryParse(this.txtCreatedDate.Text.Trim(), out tempDateTime))
            {
                podInfo.CreatedDate = DateTime.Parse(this.txtCreatedDate.Text.Trim());
            }
            else
            {
                podInfo.CreatedDate = DateTime.Now;
            }
            podInfo.PodDetailInfos = this.GetPodDetailFromGvPodDetail();
            return podInfo;
        }

        protected void gvPodDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvPodDetail.PageIndex * this.gvPodDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
            }
        }

        private void InsertPodInfo()
        {
        }

        private void LoadData()
        {
            this.lblMsg.Text = "";
            base.BindData2DDLBySysCode(this.ddlStatus, "PodStatus");
            this.ddlStatus.Items.Insert(0, new ListItem("-----", ""));
            base.BindData2DDLBySysCode(this.ddlTransportMode, "TransportMode");
            this.ddlTransportMode.Items.Insert(0, new ListItem("-----", ""));
            base.BindData2DDLBySysCode(this.ddlPickUpType, "PickupType");
            this.ddlPickUpType.Items.Insert(0, new ListItem("-----", ""));
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetPod2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPod2Form()
        {
            try
            {
                string podID = base.Request.QueryString["ID"].ToString();
                PodInfo podInfo = TransportMgtServiceClientProxy.GetInstance().GetPodInfoByID(podID);
                base.DataEntityViewState = podInfo;
                if (podInfo != null)
                {
                    this.txtPodID.Text = podID;
                    this.txtPodID.Enabled = false;
                    this.txtBolDetailID.Text = podInfo.BolID;
                    this.txtCarrierID.Text = podInfo.CarrierID;
                    this.txtOwnerID.Text = podInfo.OwnerID;
                    this.txtEquipmentID.Text = podInfo.EquipmentID;
                    this.txtDriver.Text = podInfo.DriverID;
                    this.txtExpectArriverDate.Text = base.GetPreferenceShortDate(podInfo.ExceptArriverdDate);
                    this.txtSignedDate.Text = base.GetPreferenceShortDate(podInfo.SignedDate);
                    this.txtSignedBy.Text = podInfo.SignedBy;
                    this.ddlStatus.SelectedValue = podInfo.Status;
                    if (this.ddlStatus.SelectedValue.ToUpper() == "NEW")
                    {
                        this.btnSave.Enabled = true;
                        this.btnConfirm.Enabled = false;
                    }
                    else if (this.ddlStatus.SelectedValue.ToUpper() == "SIGNOFF")
                    {
                        this.btnSave.Enabled = false;
                        this.btnConfirm.Enabled = true;
                    }
                    else
                    {
                        this.btnSave.Enabled = false;
                        this.btnConfirm.Enabled = false;
                    }
                    this.ddlTransportMode.SelectedValue = podInfo.TransportMode;
                    this.ddlPickUpType.SelectedValue = podInfo.PickupType;
                    if (!string.IsNullOrEmpty(podInfo.Podimg))
                    {
                        string[] imgUrls = podInfo.Podimg.Split(new char[] { ',' });
                        if (imgUrls.Length > 0)
                        {
                            for (int i = 0; i < imgUrls.Length; i++)
                            {
                                string temp = "Image" + ((i + 1)).ToString();
                                ((Image) base.Form.FindControl(temp)).Visible = true;
                                ((Image) base.Form.FindControl(temp)).ImageUrl = "~/Upload/Thumbnail/" + imgUrls[i].ToString();
                                string tempUrl = "../Common/showOriginal.aspx?img=" + imgUrls[i].ToString();
                                ((Image) base.Form.FindControl(temp)).Attributes.Add("onClick", "javascript:window.open ('" + tempUrl + "', 'newwindow', 'height=500px, width=800px, toolbar =no,center=yes, menubar=no, scrollbars=no, resizable=no, location=no, status=no')");
                                ((Image) base.Form.FindControl(temp)).Style.Add("cursor", "hand");
                            }
                        }
                    }
                    this.txtCustomerID.Text = podInfo.CustomerID;
                    this.txtCustomerName.Text = podInfo.CustomerName;
                    this.txtContact.Text = podInfo.Contact;
                    this.txtCPhone.Text = podInfo.CPhone;
                    this.txtCAddress.Text = podInfo.CAddress;
                    this.txtUdf1.Text = podInfo.Udf1;
                    this.txtUdf2.Text = podInfo.Udf2;
                    this.txtUdf3.Text = podInfo.Udf3;
                    this.txtUdf4.Text = podInfo.Udf4;
                    this.txtUdf5.Text = podInfo.Udf5;
                    this.txtUdf6.Text = podInfo.Udf6;
                    this.txtUdf7.Text = podInfo.Udf7;
                    this.txtUdf8.Text = podInfo.Udf8;
                    this.txtUdf9.Text = podInfo.Udf9;
                    this.txtUdf10.Text = podInfo.Udf10;
                    this.txtCreatedBy.Text = podInfo.CreatedBy;
                    this.txtCreatedDate.Text = base.GetPreferenceShortDate(podInfo.CreatedDate);
                    this.txtRemark.Text = podInfo.Remark;
                    if ((podInfo.PodDetailInfos != null) && (podInfo.PodDetailInfos.Count > 0))
                    {
                        this.hdnLineID.Value = (podInfo.PodDetailInfos[podInfo.PodDetailInfos.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvPodDetail, podInfo.PodDetailInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdatePodInfo()
        {
            try
            {
                PodInfo podInfo = base.DataEntityViewState as PodInfo;
                podInfo = this.GetPodInfo(podInfo);
                podInfo.Status = "SignOff";
                if ((podInfo.PodDetailInfos != null) && (podInfo.PodDetailInfos.Count > 0))
                {
                    for (int i = 0; i < podInfo.PodDetailInfos.Count; i++)
                    {
                        podInfo.PodDetailInfos[i].PodID = podInfo.PodID;
                        podInfo.PodDetailInfos[i].Status = "SignOff";
                        podInfo.PodDetailInfos[i].UpdatedBy = base.UserInfo.UserID;
                    }
                }
                podInfo.UpdatedBy = base.UserInfo.UserID;
                string temp = TransportMgtServiceClientProxy.GetInstance().UpdatePodInfo(podInfo);
                this.SetPod2Form();
                this.lblMsg.Text = temp + base.GetGlobalMsgResource("C004");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',4,4);</script>", false);
            }
        }
    }
}

